﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;

namespace ReportSystem_XY.Utility
{
    public static class OleDbHelper
    {
        //连接字符串
        private static readonly string conStr = ConfigurationManager.ConnectionStrings["oledbserver"].ConnectionString;
        //1、执行增(insert)删(delete)改(update)的方法
        //ExecuteNonQuery
        public static int ExecuteNonQuery(string ole, params OleDbParameter[] pms)
        {
            //创建连接，用using，等效try finaly
            using (OleDbConnection con = new OleDbConnection(conStr))
            {
                //创建连接后，需要创建命令
                using (OleDbCommand cmd = new OleDbCommand(ole, con))
                {
                    //判断参数是否不为空
                    if (pms != null)
                    {
                        //把参数加入到命令里面
                        cmd.Parameters.AddRange(pms);
                    }
                    //打开连接
                    con.Open();
                    //执行命令，返回结果
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        //2、执行查询，返回单个值的方法
        //ExecuteScalar()
        public static object ExecuteScalar(string ole, params OleDbParameter[] pms)
        {
            using (OleDbConnection con = new OleDbConnection(conStr))
            {
                using (OleDbCommand cmd = new OleDbCommand(ole, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }
        //3、执行查询，返回多行多列的方法
        //ExecuteReader()
        public static OleDbDataReader ExecuteReader(string ole, params OleDbParameter[] pms)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            using (OleDbCommand cmd = new OleDbCommand(ole, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                try
                {
                    con.Open();
                    //con没有放在using里面，如果下面的ExecuteReader出异常，就没有办法关闭con连接了
                    return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    // 执行ExecuteReader的时候，连接不能关，但执行完ExecuteReader的时候，连接需要关闭
                    //CommandBehavior.CloseConnection 只要关闭reader就会自动关闭连接
                }
                catch
                {
                    con.Close();
                    con.Dispose();
                    throw;
                    //出异常的时候才执行
                }
            }
        }
        public static DataTable ExecuteDataTable(string ole, params OleDbParameter[] pms)
        {
            using (OleDbConnection con = new OleDbConnection(conStr))
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter(ole, con);//这里面封装了SqlCommand对象
                DataSet ds = new DataSet();
                if (pms != null)
                {
                    adapter.SelectCommand.Parameters.AddRange(pms);
                }
                adapter.Fill(ds);
                return ds.Tables[0];
            }
        }
    }
}
